package com.andnux.android.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.andnux.android.MainApp;
import com.andnux.android.activity.AboutMeActivity;
import com.andnux.android.activity.LoginActivity;
import com.andnux.android.activity.MineSettingActivity;
import com.andnux.android.activity.MyOrderActivity;
import com.andnux.android.data.SystemUsers;
import com.andnux.android.databinding.FragmentMineBinding;
import com.andnux.android.dialog.DialogFactory;
import com.andnux.android.utils.DateUtils;
import com.andnux.android.utils.ImageLoader;

import org.jetbrains.annotations.NotNull;

import java.util.Date;

public class MineFragment extends BaseFragment<FragmentMineBinding> {

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.myOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), MyOrderActivity.class));
            }
        });
        binding.aboutMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), AboutMeActivity.class));
            }
        });

        binding.userLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), MineSettingActivity.class));
            }
        });

        binding.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(requireContext(), "清除成功", Toast.LENGTH_SHORT).show();
            }
        });

        binding.exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFactory.showNormalDialog(requireContext(), "温馨提示", "是否退出登录？",
                        new DialogFactory.OnSingleClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MainApp.getApp().setSystemUsers(null);
                                startActivity(new Intent(requireActivity(), LoginActivity.class));
                                requireActivity().finish();
                            }
                        });
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        SystemUsers users = MainApp.getApp().getSystemUsers();
        if (users != null) {
            binding.name.setText(users.getNickname());
            Date time = users.getCreateTime();
            if (time != null) {
                binding.time.setText(DateUtils.format(time, DateUtils.DATE_PATTERN));
            }
            ImageLoader.load(users.getAvatar(), binding.hedImage);
        }
    }
}